<?php
namespace App\Services\Activity;

use App\Exceptions\BasicException;
use App\Models\MainDB\Lottery\User as LotteryUser;
use App\Models\MainDB\Lottery2\UserBalance;
use App\Models\MainDB\UserGameExperienceLog;
use Illuminate\Support\Facades\DB;

class GameExperienceService
{
    private $gameType;
    private $settingId;
    private $playId;

    /**
     * @return int
     */
    public function getSettingId(): int
    {
        return $this->settingId;
    }

    /**
     * @param int $settingId
     */
    public function setSettingId(int $settingId): void
    {
        $this->settingId = $settingId;
    }

    /**
     * @return int
     */
    public function getPlayId(): int
    {
        return $this->playId;
    }

    /**
     * @param int $playId
     */
    public function setPlayId(int $playId): void
    {
        $this->playId = $playId;
    }

    /**
     * @param string $gameType
     */
    public function setGameType(string $gameType): void
    {
        $this->gameType = $gameType;
    }

    /**
     * @return string
     */
    public function getGameType(): string
    {
        return $this->gameType;
    }


    /**
     * @throws BasicException
     */
    private function before(int $userId): void
    {
        $logModel = new UserGameExperienceLog();
        $log = $logModel::query()->where('user_id', $userId)->first();
        if ($log) {
            throw new BasicException(60034);
        }
        if (!app('redis')->set("game:experience_receive:$userId", 'lock', 'ex', 15, 'nx')) {
            throw new BasicException(52011);
        }
    }

    private function after(int $userId): void
    {
        $logModel = new UserGameExperienceLog();
        $logModel::query()->insert([
            'user_id'    => $userId,
            'setting_id' => $this->getSettingId(),
            'play_id'    => $this->getPlayId(),
            'game_type'  => $this->getGameType(),
        ]);
    }

    /**
     * 三国
     * @throws BasicException
     */
    public function incrThreeKingdoms(int $userId): void
    {
        $this->before($userId);
        $userBalanceModel = new UserBalance();
        $userBalance = $userBalanceModel::query()->where('user_id', $userId)->lockForUpdate()->first();
        if (empty($userBalance)) {
            $userBalanceModel::query()->insert([
                'user_id'    => $userId,
                'balance_id' => 1, //小石头
                'count'      => 1,
            ]);
        } else {
            $userBalanceModel::query()->where('id', $userBalance->id)->increment('count');
        }
        $this->after($userId);
    }


    /**
     * 海岛
     * @throws BasicException
     */
    public function incrIsland(int $userId): void
    {
        $this->before($userId);
        $lotteryUserModel = new LotteryUser();
        $lotteryUser = $lotteryUserModel::query()->where('id', $userId)->lockForUpdate()->first();
        if (empty($lotteryUser)) {
            $lotteryUserModel::query()->insert([
                'id'            => $userId,
                'balance'       => 1,
                'total_balance' => 1,
            ]);
        } else {
            $extra['total_balance'] = DB::raw('total_balance+1');
            $lotteryUserModel::query()
                ->where('id', $lotteryUser->id)
                ->increment('balance', 1, $extra);
        }
        $this->after($userId);

    }


}
